package com.honey.mybatis.generator.model;

import java.util.List;
import java.util.Random;

import com.honey.compilation.java.ClassDecoration;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.core.compiler.CompilationException;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.Column;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractClass;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.criteria.CriteriaConstant;

public class ModelClass extends AbstractClass{
	
	private FullyQualifiedJavaType type = null;
	
	/**
	 * 构造函数
	 * @param clazz
	 * @param belongPlugin
	 * @param introspectSchema
	 * @param introspectSchemaColumn
	 */
	public ModelClass(
			MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn
			) {
		super(belongPlugin,introspectSchema,introspectSchemaColumn);
	}

	@Override
	protected FullyQualifiedJavaType getType() {
		if( type == null){
			type =super.getModelPrimitiveClassType(introspectSchema.getSchema());  
		}
		return type;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractClass#getSuperClass()
	 */
	@Override
	protected FullyQualifiedJavaType getSuperClass() {
		StringBuilder sb = new StringBuilder(CriteriaConstant.GENERATED_CRITERIA_CLASS_NAME)
		.append(JavaKeyWord.OPERATION_ANGLE_BRACKETS_LEFT)
		.append(type.getFullName())
		.append(JavaKeyWord.OPERATION_COMMA)
		.append(type.getFullName())
		.append(JavaKeyWord.OPERATION_$)
		.append(getTableColumnEnumClassName(introspectSchema.getSchema()))
		.append(JavaKeyWord.OPERATION_ANGLE_BRACKETS_RIGHT)
		;
		return new FullyQualifiedJavaType(sb.toString()) ;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractClass#getImplements()
	 */
	@Override
	protected FullyQualifiedJavaType[] getImplements() {
		FullyQualifiedJavaType[] answer = DEFAULT_IMPLEMENTS;
		if( introspectSchema.getSchema().isIncludeEnumColumn() ){
			answer =new FullyQualifiedJavaType[]{
					ModelConstant.IBATIS_TYPE_HANDLER
			};
		}
		return answer;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractClass#getDecoration()
	 */
	@Override
	protected ClassDecoration getDecoration() throws CompilationException {
		ClassDecoration answer = super.getDecoration();
		answer.setFinal(true);
		return answer;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractClass#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		List<Column> list = introspectSchemaColumn.allColumns();
		Random rand =  new Random() ;
		int max = list.size();
		Column tc1 = list.get(rand.nextInt(max));
    	Column tc2 = list.get(rand.nextInt(max));
    	String fieldName = super.getTableColumnEnumClassName(introspectSchema.getSchema());
    	
		String className = type.getFullName();
		String comments [] = new String[]{
				className+"继承"+CriteriaConstant.GENERATED_CRITERIA_CLASS_NAME+"类用于组装动态的SQL语句， 应用举例如下：<br />",
				"例 1: <br />",
				"<ul style=\"list-style-type:none; \">",
    			"  <li>"+ className+" m = new "+className+"();</li>",
    			"  <li>"+"Condition<"+className+"> c = new Condition<"+className+">(m);</li>" ,
    			"  <li>"+"c.or().andIdEqualTo( 1L ).andEqualTo( "+fieldName+"."+tc1.getJavaProperty().toUpperCase()+", value1).andGreaterThan("+tc2.getJavaProperty().toUpperCase()+", value2)</li>",
    			"</ul>",
    			"表示用AND连接条件,最终的SQL语句是 : WHERE id=1 AND "+tc1.getName() +" =value1 AND "+tc2.getName() +">value2",
    			"<br />",
    			"<br />",
    			"例 2: ",
    			"<ul style=\"list-style-type:none; \">",
    			"  <li>"+ className+" m = new "+className+"();</li>",
    			"  <li>"+"Condition<"+className+"> c = new Condition<"+className+">(m);</li>" ,
    			"  <li>"+"c.or().andIdEqualTo( 1L )",
    			"  <li>"+"c.or().andEqualTo( "+fieldName+"."+tc1.getJavaProperty().toUpperCase()+", value1 )</li>",
    			"  <li>"+"c.or().andGreaterThan("+tc2.getJavaProperty().toUpperCase()+", value2)</li>",
    			"</ul>",
    			"表示用OR连接条件,最终的SQL语句是 : WHERE id=1 OR "+tc1.getName() +" =value1 OR "+tc2.getName() +">value2",
    			"<br />",
    			"<br />",
    			"例 3: ",
    			"<ul style=\"list-style-type:none; \">",
    			"  <li>"+ className+" m = new "+className+"();</li>",
    			"  <li>"+"Condition<"+className+"> c = new Condition<"+className+">(m);</li>" ,
    			"  <li>"+"c.or().andIdEqualTo( 1L ).andEqualTo( "+fieldName+"."+tc1.getJavaProperty().toUpperCase()+", value1 )</li>",
    			"  <li>"+"c.or().andGreaterThan("+tc2.getJavaProperty().toUpperCase()+", value2)</li>",
    			"</ul>",
    			"表示用AND 和 OR连接条件,最终的SQL语句是 : WHERE (id=1 AND "+tc1.getName() +" =value1 ) OR "+tc2.getName() +">value2",
    			"<br />",
    			"<br />",
		};      
		
		return comments;
	}
	
}
